import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtModule } from '@nestjs/jwt';
import * as session from 'express-session';
import { PromotionService } from './service/promotion.service';
import { PromotionController } from './controller/promotion.controller';
import { CategoryController } from './controller/category.controller';
import { CategoryService } from './service/category.service';
import { Promotion } from './entities/promotion.entity';
import { Category } from './entities/category.entity';
import { Goods } from './entities/goods.entity';
import { GoodsController } from './controller/goods.controller';
import { GoodsService } from './service/goods.service';
import { New } from './entities/new.entity';
import { NewController } from './controller/new.controller';
import { NewService } from './service/new.service';
import { Account } from './entities/account.entity';
import { AccountService } from './service/account.service';
import { AccountController } from './controller/account.controller';
import { UploadModule } from './upload/upload.module';
import { AuthService } from 'src/auth/auth.service';
import { UserService } from '../custom/service/user.service';
import { User } from '../custom/entities/user.entity';
import { LocalStrategy } from 'src/auth/local.strategy';
import { JwtStrategy } from 'src/auth/jwt.strategy';
import { PassportModule } from '@nestjs/passport';
import { jwtConstants } from 'src/utils/constant';
import { CaptchaModule } from './captcha/captcha.module';
import { CaptchaService } from './captcha/captcha.service';

@Module({
  imports: [
    TypeOrmModule.forFeature(
      [User, Account, Promotion, Category, Goods, New],
      'shop',
    ),

    PassportModule,
    JwtModule.register({
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '36000s' },
    }),

    CaptchaModule,
    UploadModule,
  ],
  controllers: [
    AccountController,
    PromotionController,
    CategoryController,
    GoodsController,
    NewController,
  ],
  providers: [
    UserService,
    
    LocalStrategy,
    JwtStrategy,
    
    CaptchaService,

    AuthService,
    AccountService,
    PromotionService,
    CategoryService,
    NewService,
    GoodsService,
  ],
  exports: [
    PromotionService,
    CategoryService,
    NewService,
    GoodsService,
    AccountService,
  ],
})
export class GroceryModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(
      session({
        secret: jwtConstants.secret,
        resave: false,
        saveUninitialized: false
      })
    ).forRoutes('*')
  }
}
